package gov.fnal.xpath2ldap;

import java.util.*;
import java.io.*;
import java.net.*;

import org.apache.commons.cli.CommandLine;

import gov.fnal.xpath2ldap.util.Configuration;

/**
 * User: tdh
 * Date: Mar 4, 2009
 * Time: 4:46:11 PM
 */
public class XPathSearch {
    //String syntax = "xpathsearch [options] host_url[:port] xpathexpression1 [xpathexpression2] ...";

        //String expr = "/mall/bookstore//book[availability/@instock]/excerpt[/borkstore/@specialty=brookstone/@style]/price[@cost=10]";
        //String expr ="//Mds/GlueSE[GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp'] and GlueSEControlProtocol[@GlueSEControlProtocolType='SRM'][@GlueSEControlProtocolVersion='2.2.0']]/GlueSA/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='ilc']/../../GlueSEControlProtocol[@GlueSEControlProtocolVersion='2.2.0']/@GlueSEControlProtocolEndpoint";
        //expr ="//GlueSE[GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp'] and GlueSEControlProtocol[@GlueSEControlProtocolType='SRM'][@GlueSEControlProtocolVersion='2.2.0']]/GlueSA/GlueSAPolicy[@GlueSAPolicyFileLifeTime='permanent']/../GlueVOInfo[@GlueVOInfoAccessControlBaseRule='ilc']/../../GlueSEControlProtocol[@GlueSEControlProtocolVersion='2.2.0']/@GlueSEControlProtocolEndpoint";
        //String expr = "//GlueSE[GlueSEAccessProtocol/Status]/GlueSA[@GlueSAFreeOnlineSize>0]/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='ilc']/../../GlueSEAccessProtocol/@GlueSEAccessProtocolType";
        //expr ="//Mds/GlueSE[GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp'] and GlueSEControlProtocol[@GlueSEControlProtocolType='SRM'][@GlueSEControlProtocolVersion='2.2.0']]/GlueSA/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='ilc']/../../GlueSEControlProtocol[@GlueSEControlProtocolVersion='2.2.0']/@GlueSEControlProtocolEndpoint";
        //expr ="//Mds/GlueSE[GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp'] and GlueSEControlProtocol[@GlueSEControlProtocolType='SRM'][@GlueSEControlProtocolVersion='2.2.0']]/GlueSA/GlueVOInfo/../../GlueSEControlProtocol[@GlueSEControlProtocolVersion='2.2.0']"; // todo: search on dn if no attribute is requested
        //expr ="//GlueSE[GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp'] and GlueSEControlProtocol[@GlueSEControlProtocolType='SRM'][@GlueSEControlProtocolVersion='2.2.0']]/GlueSA/GlueSAPolicy[@GlueSAPolicyFileLifeTime='permanent']/../GlueVOInfo[@GlueVOInfoAccessControlBaseRule='ilc']/../../GlueSEControlProtocol[@GlueSEControlProtocolVersion='2.2.0']";
        //expr ="//GlueSE[GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp'] and GlueSEControlProtocol[@GlueSEControlProtocolType='SRM'][@GlueSEControlProtocolVersion='2.2.0']]/GlueSA/GlueSAPolicy[@GlueSAPolicyFileLifeTime='permanent']/../../GlueSEControlProtocol";
        //expr ="//GlueSE[GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp'] and GlueSEControlProtocol[@GlueSEControlProtocolType='SRM'][@GlueSEControlProtocolVersion='2.2.0']]/GlueSA/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='ilc']/../../GlueSEControlProtocol[@GlueSEControlProtocolVersion='2.2.0']/@GlueSEControlProtocolEndpoint";
        //expr ="//GlueSE/GlueSEControlProtocol[@GlueSEControlProtocolVersion='2.2.0']/@GlueSEControlProtocolEndpoint";
        //String expr ="//GlueSE[GlueSEControlProtocol[@GlueSEControlProtocolVersion='1.1.0'] or GlueSEControlProtocol[@GlueSEControlProtocolVersion='2.2.0']]/GlueSEControlProtocol/@GlueSEControlProtocolEndpoint";
        //String expr ="//GlueSE[GlueSEAccessProtocol/Status]/GlueSA[@GlueSAFreeOnlineSize>0]";
        static String expr ="//GlueSE/GlueSA[@GlueSAFreeOnlineSize<=0 and @GlueSAFreeOnlineSize>=0]";
        //static String expr ="//GlueSE/GlueSA[@GlueSAFreeOnlineSize!=0]";
        //String expr ="//GlueSE[GlueSEAccessProtocol/@Status and count(@valid)=2]/GlueSA[@GlueSAFreeOnlineSize>position()]";
        //String expr = "//GlueSE[GlueSEAccessProtocol/@Status='up' and Extra/@cost=5 or @valid='no']/GlueSA[@GlueSAFreeOnlineSize>0]";
        //GlueSE[GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp']
        //expr ="//GlueSE[GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp'] and GlueSEControlProtocol[@GlueSEControlProtocolType='SRM'][@GlueSEControlProtocolVersion='2.2.0']]/GlueSA/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='ilc']/../../GlueSEControlProtocol/@GlueSEControlProtocolEndpoint";
        //expr ="//GlueSE[GlueSEControlProtocol[@GlueSEControlProtocolType='SRM'][@GlueSEControlProtocolVersion='2.2.0']]/GlueSA/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='ilc']/../../GlueSEControlProtocol/@GlueSEControlProtocolEndpoint";
        //expr ="//GlueSE/GlueSA/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='engage']/../../GlueSEControlProtocol/@GlueSEControlProtocolEndpoint";
        //expr= "//GlueSE/GlueSEControlProtocol[@GlueSEControlProtocolVersion='2.2.0']/@GlueSEControlProtocolEndpoint";
        //expr= "//GlueSE[GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp']]/GlueSEControlProtocol[@GlueSEControlProtocolType='SRM' and @GlueSEControlProtocolVersion='2.2.0']/@GlueSEControlProtocolEndpoint";
        //expr= "//GlueSE[GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp' or @GlueSEAccessProtocolType='gsidcap']]/GlueSA/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='dzero']/../../GlueSEControlProtocol[@GlueSEControlProtocolType='SRM' and @GlueSEControlProtocolVersion='2.2.0']/@GlueSEControlProtocolEndpoint";
        //expr ="//GlueSE/GlueSA/GlueSAPolicy[@GlueSAPolicyFileLifeTime='permanent']";
        //static String expr = "//GlueSubCluster[@GlueHostProcessorModel][@GlueHostOperatingSystemName]";
        //expr = "//GlueCE[@GlueCEAccessControlBaseRule='ilc']/../GlueSE[GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp'] and GlueSEControlProtocol[@GlueSEControlProtocolType='SRM'][@GlueSEControlProtocolVersion='2.2.0']]/GlueSA/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='ilc']/../../GlueSEControlProtocol[@GlueSEControlProtocolVersion='2.2.0']/@GlueSEControlProtocolEndpoint";
        //expr = "//GlueCE[@GlueCEAccessControlBaseRule='ilc']";
        //expr = "//GlueCE/@GlueCEName";
        //static String expr ="//GlueSE/GlueSA/GlueVOInfo/@GlueVOInfoAccessControlBaseRule";
       // static String expr ="//GlueSE/GlueSA/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='engage']";
        //static String expr ="//GlueSE/GlueSEControlProtocol[@GlueSEControlProtocolVersion='2.2.0'][@GlueSEControlProtocolEndpoint]/../GlueSA[@GlueSAFreeOnlineSize>0]/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='engage'][@GlueVOInfoPath]";
       //ldapsearch -x -LLL -H ldap://is.grid.iu.edu:2170 -b GlueSEUniqueID=ff-srm.unl.edu,Mds-Vo-name=Nebraska,Mds-Vo-name=local,o=grid "(&(objectclass=GlueVOInfo)(GlueVOInfoAccessControlBaseRule=engage))"
        //expr ="//GlueSE/GlueSEControlProtocol/@GlueSEControlProtocolEndpoint";
    //private int s_port = LDAPConnection.DEFAULT_PORT;
     //static String expr ="//Mds";
    //"//GlueSE[@GlueSEUniqueID='abitibi.sbgrid.org']/GlueSEAccessProtocol[@GlueSEAccessProtocolType='gsiftp']"
    //static String expr ="//GlueSE[@GlueSEUniqueID='srm.ihepa.ufl.edu']/GlueSA/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='engage']";
    //static String expr ="//GlueSE[@GlueSEUniqueID]/GlueSA/GlueVOInfo[@GlueVOInfoAccessControlBaseRule='engageXXX']";

    public static void main (String[] args) {

        if(args.length==0)  {
            args = new String[]{"--help"};
        }

        String responses = processXPathExpression(args);
        if(responses.equals("0\n")) {
          System.err.println(responses);
        } else {
          System.out.println(responses);
        }
    }

    public static String processXPathExpression(String[] args) {
        CommandLine cmdline = XPathSearchOptions.processArguments(args);
        return processXPathExpression(cmdline);
    }

    @SuppressWarnings("unchecked")
	public static String processXPathExpression(CommandLine cmdline) {
        String responses = "";
        XPathLDAPEvaluator xpathSearcher = new XPathLDAPEvaluator(cmdline);
        List<String> argslist = cmdline.getArgList();
        if(cmdline.hasOption("test")) {
            String xpath_expression = expr;
            System.err.println(xpath_expression);
            responses = xpathSearcher.search(xpath_expression);
        } else {
            for (String xpath_expression : argslist) {
                System.err.println(xpath_expression);
                responses += xpathSearcher.search(xpath_expression);
            }
        }

        return responses;
    }


}



